// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
#pragma once
#include <agents.h>

namespace Weathr
{
    namespace Web
    {
        class HttpRequestThrottler : public concurrency::agent
        {
        public:
            HttpRequestThrottler(uint32_t maxRequestsPerSecond);
            concurrency::task<web::http::http_response> DownloadAsync(const web::http::uri& requestUri);
        protected:
            void run();
        private:
            void HandleRequest(const std::tuple<web::http::uri, concurrency::task_completion_event<web::http::http_response>> &request);

            const uint32_t m_maxRequestsPerSecond;
            std::atomic<uint32_t> m_activeRequests;
            concurrency::event m_waitEvent;

            concurrency::unbounded_buffer<std::tuple<web::http::uri, concurrency::task_completion_event<web::http::http_response>>> m_requests;
            std::unique_ptr<concurrency::call<std::tuple<web::http::uri, concurrency::task_completion_event<web::http::http_response>>>> m_requestHandler;
        };
    }
}